var _ISBOND = false;

$(function() {
    var loading = weui.loading('loading', { className: 'custom-classname' });
	var result = Model.gift.nextWeekAd();
	if (result.code) {
		Common.fn.formatOutput(result);
		loading.hide();
		return false;
    }
    var res = result.data;
    _ISBOND = res.bond;
    for(var i = 0;i < res.advertise.length;i++) {
        var item = '<div class="inte-ad-list" data-id="' + res.advertise[i].id + '"><p><span class="text-1-6">广告位' + parseInt(i + 1) + '</span><span class="inte-ad-now text-1-4">目前出价：' + res.advertise[i].price + '</span>';
        switch(res.advertise[i].state) {
            case '未出价':
                item += '<span class="inte-ad-state text-1-8" style="color: #0000ff;">' + res.advertise[i].state + '</span></p></div>';
                break;
            case '竞拍中':
                item += '<span class="inte-ad-state text-1-8" style="color: #ff0000;">' + res.advertise[i].state + '</span></p></div>';
                break;
            default:
                item += '<span class="inte-ad-state text-1-8">' + res.advertise[i].state + '</span></p></div>';
                break;
        }
        ;
        $('.inte-ad-lists').append(item);
    }
    // 联系客服
    $('#showTooltips').attr('href', 'tel:' + res.phone);
    loading.hide();
});

// 竞拍广告位
$(document).on('click', '.inte-ad-list', function() {
    // 判断是否可以出价
    if(!_ISBOND) {
        weui.confirm(
            '您当前尚未缴纳保证金，是否前往缴纳？',
			{
				title: '系统提示',
				buttons: [
					{
						label: '取消',
						type: 'default',
						onClick: function() {
							console.log('no');
						},
					},
					{
						label: '缴纳',
						type: 'primary',
						onClick: function() {
							location.href = '../views/bond.html';
						},
					},
				],
			}
		);
    }
    var id = $(this).attr('data-id');
	weui.confirm(
		'加价金额只能为10的倍数<input type="number" id="toPrice" style="border: 0;font-size: 3rem;text-align: center;width: 100%;border-bottom: 1px solid #000;">',
		{
			title: '请输入加价金额',
			buttons: [
				{
					label: '取消',
					type: 'default',
					onClick: function() {
						console.log('no');
					},
				},
				{
					label: '确认出价',
					type: 'primary',
					onClick: function() {
                        var price = $.trim($('#toPrice').val());
                        if(price % 10 != 0 || price <= 0) {
                            alert('加价金额不符合规范！');
                            return false;
                        }
                        var loading = weui.loading('loading', {
							className: 'custom-classname',
						});
						var result = Model.gift.offerPrice({
							advertiseId: id,
							price: price,
						});
						if (result.code) {
							alert(result.message);
							confirmDom.hide();
							return false;
						}
						alert(result.data);
						location.reload();
						loading.hide();
					},
				},
			],
		}
	);
});